package com.ywu.application.standard.dataSource.source;

import com.ywu.boot.exception.YwuRuntimeException;
import com.ywu.application.standard.dataSource.entity.YwuDataExecEntity;
import com.ywu.application.standard.dataSource.entity.YwuDataSourceEntity;
import com.ywu.application.standard.dataSource.entity.YwuDataTableEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName YwuExecSqlCoreService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/10/25 20:34
 * @Version 1.0
 **/
@Component
public class YwuExecSqlCoreService {

    private Map<String, YwuExecSqlCore> execSqlCoreMap;

    @Autowired
    private List<YwuExecSqlCore> execSqlCoreList;

    /**
    * @Author GroundDemo
    * @Description 初始换sql执行引擎Map
    * @Date 10:38 2024/10/26
    * @Param []
    * @return void
    **/
    public void initEngineMap() {
        // 如果当前engineMap不为空的话，表示已初始化
        if (!CollectionUtils.isEmpty(execSqlCoreMap)) {
            return;
        }
        execSqlCoreMap = new HashMap<>();
        for (YwuExecSqlCore execSqlCore : execSqlCoreList) {
            String dataSourceType = execSqlCore.dataSourceType();
            execSqlCoreMap.put(dataSourceType, execSqlCore);
        }
    }

    /**
    * @Author GroundDemo
    * @Description 创建数据源
    * @Date 20:36 2024/10/25
    * @Param [ywuDataSourceEntity]
    * @return void
    **/
    public void createDataSource(YwuDataSourceEntity ywuDataSourceEntity) {
        initEngineMap();
        // 根据类型回去对应的数据库处理类
        if (!execSqlCoreMap.containsKey(ywuDataSourceEntity.getDataSourceType())) {
            throw new YwuRuntimeException("data source is not exists");
        }
        YwuExecSqlCore execSqlCore = execSqlCoreMap.get(ywuDataSourceEntity.getDataSourceType());
        execSqlCore.createDataSource(ywuDataSourceEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 修改数据源
    * @Date 10:10 2024/10/26
    * @Param [ywuDataSourceEntity]
    * @return void
    **/
    public void updateDataSource(YwuDataSourceEntity ywuDataSourceEntity) {
        initEngineMap();
        // 根据类型回去对应的数据库处理类
        if (!execSqlCoreMap.containsKey(ywuDataSourceEntity.getDataSourceType())) {
            throw new YwuRuntimeException("data source is not exists");
        }
        YwuExecSqlCore execSqlCore = execSqlCoreMap.get(ywuDataSourceEntity.getDataSourceType());
        execSqlCore.updateDataSource(ywuDataSourceEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 删除数据源
    * @Date 10:17 2024/10/26
    * @Param [ywuDataSourceEntity]
    * @return void
    **/
    public void deleteDataSource(YwuDataSourceEntity ywuDataSourceEntity) {
        initEngineMap();
        // 根据类型回去对应的数据库处理类
        if (!execSqlCoreMap.containsKey(ywuDataSourceEntity.getDataSourceType())) {
            throw new YwuRuntimeException("data source is not exists");
        }
        YwuExecSqlCore execSqlCore = execSqlCoreMap.get(ywuDataSourceEntity.getDataSourceType());
        execSqlCore.deleteDataSource(ywuDataSourceEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 创建数据表
    * @Date 17:40 2024/10/28
    * @Param [ywuDataTableEntity]
    * @return void
    **/
    public void createTable(YwuDataTableEntity ywuDataTableEntity) {
        initEngineMap();
        // 根据类型回去对应的数据库处理类
        if (!execSqlCoreMap.containsKey(ywuDataTableEntity.getDataSourceType())) {
            throw new YwuRuntimeException("data source is not exists");
        }
        YwuExecSqlCore execSqlCore = execSqlCoreMap.get(ywuDataTableEntity.getDataSourceType());
        execSqlCore.createTable(ywuDataTableEntity);
    }

    /**
     * @Author GroundDemo
     * @Description 修改数据表
     * @Date 17:35 2024/10/28
     * @Param [ywuDataTableEntity]
     * @return void
     **/
    public void updateTable(YwuDataTableEntity oldEntity, YwuDataTableEntity newEntity) {
        initEngineMap();
        // 根据类型回去对应的数据库处理类
        if (!execSqlCoreMap.containsKey(newEntity.getDataSourceType())) {
            throw new YwuRuntimeException("data source is not exists");
        }
        YwuExecSqlCore execSqlCore = execSqlCoreMap.get(newEntity.getDataSourceType());
        execSqlCore.updateTable(oldEntity, newEntity);
    }

    /**
     * @Author GroundDemo
     * @Description 删除数据表
     * @Date 17:35 2024/10/28
     * @Param [ywuDataTableEntity]
     * @return void
     **/
    public void deleteTable(YwuDataTableEntity ywuDataTableEntity) {
        initEngineMap();
        // 根据类型回去对应的数据库处理类
        if (!execSqlCoreMap.containsKey(ywuDataTableEntity.getDataSourceType())) {
            throw new YwuRuntimeException("data source is not exists");
        }
        YwuExecSqlCore execSqlCore = execSqlCoreMap.get(ywuDataTableEntity.getDataSourceType());
        execSqlCore.deleteTable(ywuDataTableEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 插入数据
    * @Date 14:00 2024/11/20
    * @Param [ywuDataExecEntity]
    * @return void
    **/
    public void insertIntoInfo(YwuDataExecEntity ywuDataExecEntity) {
        initEngineMap();
        // 根据类型回去对应的数据库处理类
        if (!execSqlCoreMap.containsKey(ywuDataExecEntity.getDataSourceType())) {
            throw new YwuRuntimeException("data source is not exists");
        }
        YwuExecSqlCore execSqlCore = execSqlCoreMap.get(ywuDataExecEntity.getDataSourceType());
        execSqlCore.insertIntoInfo(ywuDataExecEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 删除数据
    * @Date 14:01 2024/11/20
    * @Param [ywuDataExecEntity]
    * @return void
    **/
    public void deleteInfo(YwuDataExecEntity ywuDataExecEntity) {
        initEngineMap();
        // 根据类型回去对应的数据库处理类
        if (!execSqlCoreMap.containsKey(ywuDataExecEntity.getDataSourceType())) {
            throw new YwuRuntimeException("data source is not exists");
        }
        YwuExecSqlCore execSqlCore = execSqlCoreMap.get(ywuDataExecEntity.getDataSourceType());
        execSqlCore.deleteInfo(ywuDataExecEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 修改数据
    * @Date 14:01 2024/11/20
    * @Param [ywuDataExecEntity]
    * @return void
    **/
    public void updateInfo(YwuDataExecEntity ywuDataExecEntity) {
        initEngineMap();
        // 根据类型回去对应的数据库处理类
        if (!execSqlCoreMap.containsKey(ywuDataExecEntity.getDataSourceType())) {
            throw new YwuRuntimeException("data source is not exists");
        }
        YwuExecSqlCore execSqlCore = execSqlCoreMap.get(ywuDataExecEntity.getDataSourceType());
        execSqlCore.updateInfo(ywuDataExecEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 查询数据
    * @Date 14:02 2024/11/20
    * @Param [ywuDataExecEntity]
    * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
    **/
    public List<Map<String, Object>> queryInfo(YwuDataExecEntity ywuDataExecEntity) {
        initEngineMap();
        // 根据类型回去对应的数据库处理类
        if (!execSqlCoreMap.containsKey(ywuDataExecEntity.getDataSourceType())) {
            throw new YwuRuntimeException("data source is not exists");
        }
        YwuExecSqlCore execSqlCore = execSqlCoreMap.get(ywuDataExecEntity.getDataSourceType());
        return execSqlCore.queryInfo(ywuDataExecEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 复杂查询数据
    * @Date 15:51 2025/9/13
    * @Param [ywuDataExecEntity]
    * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
    **/
    public List<Map<String, Object>> complexQueryInfo(YwuDataExecEntity ywuDataExecEntity) {
        initEngineMap();
        // 根据类型回去对应的数据库处理类
        if (!execSqlCoreMap.containsKey(ywuDataExecEntity.getDataSourceType())) {
            throw new YwuRuntimeException("data source is not exists");
        }
        YwuExecSqlCore execSqlCore = execSqlCoreMap.get(ywuDataExecEntity.getDataSourceType());
        return execSqlCore.complexQueryInfo(ywuDataExecEntity);
    }
}
